﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    dbConn db = new dbConn();
    string agent;
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
          if ( !IsPostBack)
            db.populateAgent(ddAgent);
       
    }
    protected void ddAgent_SelectedIndexChanged(object sender, EventArgs e)
    {
               agent = ddAgent.SelectedValue.ToString().Trim();
                
    }

    private void BindGrid(string agent, string edate)
    {
         string connString = ConfigurationManager.ConnectionStrings["TEST"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            string sql = " SELECT * FROM [Commission].[dbo].[test] where AgentAgencyAccount = @agent and EndingDate= @edate ";
            SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
            //SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter param1 = new SqlParameter("@agent", agent);
            ada.SelectCommand.Parameters.Add(param1);
            SqlParameter param2 = new SqlParameter("@edate", edate);
            ada.SelectCommand.Parameters.Add(param2);

            conn.Open();

            DataTable dt = new DataTable();
            
            ada.Fill(dt);
            gv.DataSource = dt;
            gv.DataBind();
          
        }
    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
       
           BindGrid(ddAgent.SelectedValue.ToString().Trim(), ddEdate.SelectedItem.Text.Trim());
    }
   
    protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            if (ddEdate.SelectedItem.Text != "")
            {
               
               Label lbl = (Label)e.Row.FindControl("lblDate");
                lbl.Text = ddEdate.SelectedItem.Text.Trim();
            }
        }
    }
    protected string hideAccNo(string acc)
    {
        string hiddenString = acc.Substring(acc.Length - 4)
        .PadLeft(acc.Length, '*');


        return hiddenString;

    }
   
}
